﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace KALE_Scanning {
    public static class Crypt {
        /// <summary>
        /// orjinal texti şifreleyip döndürür.
        /// </summary>
        /// <param name="plainText">orjinal text</param>
        /// <param name="passPhrase">şifre</param>
        /// <returns>şifrelenmiş text</returns>
        public static string Encrypt(string plainText) {
            const string passPhrase = "123";
            const string saltValue = "s@1tValue";
            const string hashAlgorithm = "SHA1";
            const int passwordIterations = 2;
            const string initVector = "@1B2c3D4e5F6g7H8";
            const int keySize = 256;

            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);

            byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

            PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);

            byte[] keyBytes = password.GetBytes(keySize / 8);

            RijndaelManaged symmetricKey = new RijndaelManaged();

            symmetricKey.Mode = CipherMode.CBC;

            ICryptoTransform encryptor = symmetricKey.CreateEncryptor(
                                                             keyBytes,
                                                             initVectorBytes);

            MemoryStream memoryStream = new MemoryStream();

            CryptoStream cryptoStream = new CryptoStream(memoryStream,
                                                         encryptor,
                                                         CryptoStreamMode.Write);
            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);

            cryptoStream.FlushFinalBlock();

            byte[] cipherTextBytes = memoryStream.ToArray();

            memoryStream.Close();
            cryptoStream.Close();

            string cipherText = Convert.ToBase64String(cipherTextBytes);

            return cipherText;
        }

        /// <summary>
        /// şifreli texti orjinaline döndürür.
        /// </summary>
        /// <param name="cipherText">şifrelenmiş text</param>
        /// <param name="passPhrase">şifre</param>
        /// <returns>orjinal text</returns>
        public static string Decrypt(string cipherText) {
            try
            {
                const string passPhrase = "123";
                const string saltValue = "s@1tValue";
                const string hashAlgorithm = "SHA1";
                const int passwordIterations = 2;
                const string initVector = "@1B2c3D4e5F6g7H8";
                const int keySize = 256;

                byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
                byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);

                byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
                PasswordDeriveBytes password = new PasswordDeriveBytes(
                                                                passPhrase,
                                                                saltValueBytes,
                                                                hashAlgorithm,
                                                                passwordIterations);

                byte[] keyBytes = password.GetBytes(keySize / 8);

                RijndaelManaged symmetricKey = new RijndaelManaged();

                symmetricKey.Mode = CipherMode.CBC;

                ICryptoTransform decryptor = symmetricKey.CreateDecryptor(
                                                                 keyBytes,
                                                                 initVectorBytes);

                MemoryStream memoryStream = new MemoryStream(cipherTextBytes);

                CryptoStream cryptoStream = new CryptoStream(memoryStream,
                                                              decryptor,
                                                              CryptoStreamMode.Read);

                byte[] plainTextBytes = new byte[cipherTextBytes.Length];

                int decryptedByteCount = cryptoStream.Read(plainTextBytes,
                                                           0,
                                                           plainTextBytes.Length);

                memoryStream.Close();
                cryptoStream.Close();

                string plainText = Encoding.UTF8.GetString(plainTextBytes,
                                                           0,
                                                           decryptedByteCount);

                return plainText;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Error Message : \n" + ex.Message + Environment.NewLine + "Error Stack Trace : \n" + ex.StackTrace);
                return "";
            }
        }

    }





}
